package com.qimai;

import com.alibaba.fastjson.JSONObject;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class QiMaiTest {
    private static String HOST = "https://api.qimai.cn";
    private static String DOMAIN = "https://www.qimai.cn";

    public static void main(String[] args) throws Exception {
        // brand=free&device=ipad&country=cn&genre=36
        RankParam paramRank = new RankParam();
        paramRank.setBrand("free");
        paramRank.setDevice("ipad");
        paramRank.setCountry("cn");
        paramRank.setGenre("36");
//        paramRank.setDate("2021-06-23");
//        paramRank.setPage("1");
        String path = "/rank/index";
        paramRank.setAnalysis(path);

        
        JSONObject json = req(path, paramRank.param());
        System.out.println("响应：" + json);
    }



    public static JSONObject req(String path, String param) throws Exception {
        String url = String.format("%s%s?%s", HOST, path, param);
//        String ip = ProxyUtils.get();
        String ip = "127.0.0.1:1087";
        System.out.printf("【%s】=> %s\n", ip, url);
//        OkHttpClient client = Functions.getOkHttpClient(ip);
        OkHttpClient client = new OkHttpClient();
        Request.Builder builder = new Request.Builder().url(url);
        Request request = builder.headers(getHeaders()).get().build();
        Response response = client.newCall(request).execute();
        return JSONObject.parseObject(response.body().string());
    }

    private static Headers getHeaders() {
        return new Headers.Builder()
                .add("user-agent", UAS.get(random.nextInt(UAS.size())))
//                .add("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36")
//                .add("Referer", DOMAIN + "/")
//                .add("Origin", DOMAIN)
//                .add("cookie", "PHPSESSID=t7grs6jee5vggdaaqjkks8r4df; qm_check=SxJXQEUSChd2fHd1dRQQeV5EVVwcEHxZRlVVGGYREHh0EHdCUUVfW1NKEAAOAAAQdFlCVVZDAXQAdUwYRkNvA28AEEVEbQNmABgYWVdURV1UWVgBBh5dXFgVAQAeAQgeAQUZBgQLBR0aHBJTWFVTWxcNEAAVABgIHAAcABJN; gr_user_id=275eea8f-8a94-40ab-acf1-be0723ba0bd5; ada35577182650f1_gr_session_id=2237df12-b39e-4829-a4f6-4414094c691b; ada35577182650f1_gr_session_id_2237df12-b39e-4829-a4f6-4414094c691b=true; synct=1638351137.575; syncd=-50054")
                .build();
    }

    public static Random random = new Random();
    public static List<String> UAS = Arrays.asList(
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
            "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
            "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
            "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
            "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
            "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
            "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
            "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
            "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52"
    );
}
